<?php
/**
 * Here are all the main function of this site
 * some other functions may be found in the code
 * but they are only there becouse they are used
 * only once!
 */

/**
 * Takes a get() page and creates an XML and then xPath the XML
 * REMEMBER: /tbody/ SHOULD NOT be used in xPath, just skip it
 */
function xPathify(&$content, $path) {
	// Silencing with @ is not efficient, but currently only way I know how
	$doc   = @DOMDocument::loadHTML($content);
	$xpath = new DOMXPath($doc);
	$res   = $xpath->query($path);

	return $res;
}

/**
 * Returns the current schoolyear
 * Return format: yyyyxxxx (where xxxx = yyyy+1)
 */
function getSchoolYear() {
	if (date('m') < 7) {
		$result = (date('Y')-1)."".date('Y');
	}
	else $result = date('Y')."".(date('Y')+1);
	return $result;
}

/**
 * Works on node-reference, modifies node real-time
 * no need to return data.
 *
 * Used to remove newlines and tabs in node-data
 * can be useful for cleaner data-output and
 * matching data with regexes.
 */
function trimNodeValues(&$node) {
	foreach ($node as $n) {
		// Replace newlines and tabs
		$to_replace = array("\n", "\r", "\t");
		$n->nodeValue = str_replace($to_replace, '', $n->nodeValue);
	}
}

// Used to strip slashes from POST-data
function stripPost(&$arr) {
	foreach ($arr as $key => $val) {
		if (is_array($arr[$key]))
			stripPost($arr[$key]);
		else
			$arr[$key] = stripslashes($val);
	}
}

// If them darned magic quotes are enabled, run stripPost function
if (get_magic_quotes_gpc()) {
	stripPost($_POST);
}

// Parameter: array (String)
// This functions takes an array of error messages and prints them nicely on the screen
function getFeedback( $feedback ) {
	if( !count( $feedback ) )
	{
		return '';
	}
	else
	{
		$ret = '<div id="ui-widget"><div class=ui-state-error ui-corner-all" style="padding: 0 .7em;">  <ul>';
		foreach( $feedback as $f )
		{
			$ret .= '<span class="ui-icon ui-icon-alert" style="float: left; margin-right: .3em;"></span><p><strong>Error: </strong>' . $f . '</p>';	
		}
		$ret .= ' </div</div>';

		unset($_SESSION['feedback']);
		session_unregister($feedback); 

		return $ret;
	}	
}

